package com.example.pathprotectsystem.service.impl;

import com.example.pathprotectsystem.mapper.PathMapper;
import com.example.pathprotectsystem.pojo.AskList;
import com.example.pathprotectsystem.pojo.PageBean;
import com.example.pathprotectsystem.pojo.Path;
import com.example.pathprotectsystem.service.PathService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PathServiceImpl implements PathService {
    @Autowired
    private PathMapper pathMapper;

    @Override
    public void addpath(Path path) {
        pathMapper.addpath(path);
    }

    @Override
    public PageBean<Path> pathlist(AskList askList) {
        //创建PageBean对象
        PageBean<Path> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<Path> as = pathMapper.pathlist(str);
        System.out.println(as.size());
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<Path> p = (Page<Path>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void pathupdate(Path path) {
        pathMapper.pathupdate(path);
    }

    @Override
    public void pathdelete(int id) {
        pathMapper.pathdelete(id);
    }

    @Override
    public List<String> getpaths() {
        List<String> p = pathMapper.getpaths();
        return p;
    }

    @Override
    public int findPathIdByPathName(String pathName) {
        int pathid = pathMapper.findPathIdByPathName(pathName);
        return pathid;
    }

    @Override
    public List<String> pathall() {
        List<String> p = pathMapper.pathall();
        return p;
    }

    @Override
    public Path findPathByName(String pathName) {
        Path p = pathMapper.findPathByName(pathName);
        return p;
    }

    @Override
    public int findPathByPathName(String name) {
        return pathMapper.findPathByPathName(name);
    }

    @Override
    public List<Path> getallpath() {
        return pathMapper.getallpath();
    }
}
